//
// Created by ct on 2020/10/27.
//

#ifndef NETTOP_MT_LIST_H
#define NETTOP_MT_LIST_H
#include <mutex>
#include <list>
#include <algorithm>

template<typename T>
class mt_list {

    std::mutex	mtx_;
    std::list<T>	list_;
public:
    mt_list(const mt_list&) = delete;
    mt_list& operator=(const mt_list&) = delete;

    mt_list()=default;

    ~mt_list() = default;

    void push(const T& in) {
        std::lock_guard<std::mutex>	lg(mtx_);
        list_.push_back(in);
    }

    void push_many(const std::list<T>& in) {
        std::lock_guard<std::mutex>	lg(mtx_);
        std::for_each(in.begin(), in.end(), [&](const T& t){ list_.push_back(t); });
    }

    void swap(std::list<T>& out) {
        std::lock_guard<std::mutex>	lg(mtx_);
        list_.swap(out);
    }
};
#endif //NETTOP_MT_LIST_H
